package kines.ct;

import kines.SerializableDB;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.sql.*;

/**
 * <p>Title: CT</p>
 * <p>Description: Intranet Consorcio Terrenos</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Consorcio Terrenos</p>
 * @author Jose Delgado
 * @version 1.0
 */

public class QuotationSalesPlan implements SerializableDB {
  /**
   * Sales Plan
   */
  private SalesPlan salesPlan = null;
  /**
   * Quotation Id
   */
  private Quotation quotation = null;
  /**
   * Sets Quotation Identifier
   * @param quotationId
   */
  public void setQuotationId( int quotationId )throws Exception{
    this.quotation = Quotation.getData( quotationId );
  }
  /**
   * Sets Quotation
   * @param quotation
   */
  public void setQuotation( Quotation quotation ){
    this.quotation = quotation;
  }
  /**
   * Gets Quotation Identifier
   * @return quotationId
   */
  public Quotation getQuotation( ){
    return quotation;
  }
  /**
   * Sets Plan Number and Development Code
   * @param number of plan
   * @param developmentCodes
   */
  public void setSalesPlan( int number, String developmentCode )throws Exception{
    salesPlan = SalesPlan.getData( number, developmentCode );
  }
  /**
   * Gets Sales Plan
   * @return Sales Plan
   */
  public SalesPlan getSalesPlan( ){
    return salesPlan;
  }
  /**
   * Gets DownPayment
   * @return downPayment
   * @throws java.lang.Exception
   */
  public double getDownPayment( )throws Exception{
    double downPayment = 0;
    Terrain terreno = quotation.getTerrain();
    double totalPrice = terreno.getPricePerMeter()*terreno.getArea();
    if( salesPlan.getTerm() == 0 ){
      downPayment = ( totalPrice ) * ( 1 - (salesPlan.getCashPayment()/100) );
    }else{
      downPayment = ( totalPrice ) * ( salesPlan.getDownPayment()/100 );
    }
    return downPayment;
  }
  /**
   * Gets Monthly Payment
   * @return Monthly Payment
   */
  public double getMonthlyPayment( )throws Exception{
    double monthlyPayment = 0;
    Terrain terreno = quotation.getTerrain();
    if( salesPlan.getTerm() != 0 ){
      double totalPrice = terreno.getPricePerMeter()*terreno.getArea();
      if( salesPlan.getMonthlyInterest() == 0 ){
        monthlyPayment = ( (totalPrice-getDownPayment())/salesPlan.getTerm() )+((totalPrice-getDownPayment())*salesPlan.getInsurance())+salesPlan.getManagementCost( );
      }else{
        monthlyPayment = ( ( ( totalPrice-getDownPayment() ) * (salesPlan.getMonthlyInterest()/100) ) / ( 1 - Math.pow(1+(salesPlan.getMonthlyInterest()/100),-salesPlan.getTerm()) ) )
            +( (totalPrice-getDownPayment())*salesPlan.getInsurance())+salesPlan.getManagementCost( );
      }
    }
    return monthlyPayment;
  }
  /**
   * Gets Plans of that Quotation Id
   * @param quotationId Identifier of Quotation
   * @return Plans for that qutotation id
   */
  public static Object[] getPlansAll( int quotationId )throws Exception{
    ArrayList plans = new ArrayList( );
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn, username,password);
    Statement stmt_quotation_plan = ConnRs.createStatement();
    //String str_quotation_plan = "select * from c_cotizacion_plan where id_cotizacion = "+quotationId;
    String str_quotation_plan = "select "+quotationId+" as id_cotizacion, iddesarrollo as cvedesarrollo, idplan, noplan, desctocontado, porcenganche, plazo, intmensual, gastosadmon from planesfinanciamiento where iddesarrollo = ( select cvedesarrollo from c_cotizacion where id_cotizacion = "+quotationId+" ) and NoPlan > 0";
    ResultSet rs_quotation_plan = stmt_quotation_plan.executeQuery(str_quotation_plan);
    while( rs_quotation_plan.next() ){
      plans.add( QuotationSalesPlan.getQuotationSalesPlan( rs_quotation_plan ) );
    }
    ConnRs.close();
    return plans.toArray();
  }
  /**
   * Gets Plans of that Quotation Id
   * @param quotationId Identifier of Quotation
   * @return Plans for that qutotation id
   */
  public static Object[] getPlans( int quotationId )throws Exception{
    ArrayList plans = new ArrayList( );
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn, username,password);
    Statement stmt_quotation_plan = ConnRs.createStatement();
    String str_quotation_plan = "select * from c_cotizacion_plan where id_cotizacion = "+quotationId;
    ResultSet rs_quotation_plan = stmt_quotation_plan.executeQuery(str_quotation_plan);
    while( rs_quotation_plan.next() ){
      plans.add( QuotationSalesPlan.getQuotationSalesPlan( rs_quotation_plan ) );
    }
    ConnRs.close();
    return plans.toArray();
  }
  /**
   * Fill QuotationSalesPlan from ResultSet
   * @param rs_quotation_plan to fill object
   * @return Object QuotationSalesPlan
   * @throws java.lang.Exception
   */
  private static QuotationSalesPlan getQuotationSalesPlan( ResultSet rs_quotation_plan )throws Exception{
    QuotationSalesPlan qs = null;
    if( rs_quotation_plan != null ){
      qs = new QuotationSalesPlan( );
      qs.setQuotationId( rs_quotation_plan.getInt("id_cotizacion") );
      qs.setSalesPlan( rs_quotation_plan.getInt("NoPlan"), rs_quotation_plan.getString("cveDesarrollo") );
    }
    return qs;
  }
  /**
   * Save data of this quotation for this sales plan
   * @param session to save message attibute
   * @throws java.lang.Exception
   */
  public void saveData(HttpSession session) throws Exception {
    if( quotation != null  && salesPlan != null){
      Driver DriverRs = (Driver)Class.forName(driver).newInstance();
      Connection ConnRs = DriverManager.getConnection(strconn, username,
          password);
      Statement stmt_qsp = ConnRs.createStatement();
      String str_qsp = "INSERT INTO c_cotizacion_plan (id_cotizacion, NoPlan, CveDesarrollo) "+
          " VALUES ("+quotation.getId()+", "+salesPlan.getNumber()+", '"+salesPlan.getDevelopmentCode().getCode()+"') ";
      stmt_qsp.executeUpdate( str_qsp );
    }
  }
  /**
   * Not Implement
   * @return
   * @throws java.lang.Exception
   */
  public boolean deleteData() throws Exception {
    /**@todo Implement this kines.SerializableDB method*/
    throw new java.lang.UnsupportedOperationException("Method deleteData() not yet implemented.");
  }
}
